package com.bafa.services;

import java.sql.ResultSet;

import com.bafa.jdbc.DBAccess;
import com.bafa.jdbc.QueryObject;
import com.bafa.jdbc.UpdateObject;
import com.bafa.objects.Match;
import com.bafa.tables.Keys;

public class LeagueTableServices {

	static void removeResultFromLeague(DBAccess db, int matchId){
		Match match = RetrieveServices.getMatch(matchId);
		
		QueryObject queryObj5 = new QueryObject(Keys.LEAGUE);
		if(match!=null){
			int seasonId = match.getSeasonId();
			int division = match.getDivision();
			int teamA = match.getTeamA();
			int teamB = match.getTeamB();
			int goalsA = match.getGoalsA();
			int goalsB = match.getGoalsB();
			queryObj5.addWhereAndField("SeasonID",new Integer(seasonId).toString());
			
			removeTeamLeagueResult(db,queryObj5,goalsA,goalsB,teamA);
			removeTeamLeagueResult(db,queryObj5,goalsB,goalsA,teamB);
			
			
		}
		
	}

	
	static void updateLeague(DBAccess db, int matchId, int goalsA, int goalsB){
		Match match = RetrieveServices.getMatch(matchId);
		
		QueryObject queryObj5 = new QueryObject(Keys.LEAGUE);
		if(match!=null){
			int seasonId = match.getSeasonId();
			int division = match.getDivision();
			int teamA = match.getTeamA();
			int teamB = match.getTeamB();
			
			queryObj5.addWhereAndField("SeasonID",new Integer(seasonId).toString());
			
			updateTeamLeague(db,queryObj5,goalsA,goalsB,teamA);
			queryObj5.setQueryString("");
			updateTeamLeague(db,queryObj5,goalsB,goalsA,teamB);
			
			
		}
		
	}

	static void removeTeamLeagueResult(DBAccess db, QueryObject query, int aGoalsFor, int aGoalsAgainst, int teamId){
		UpdateObject update = new UpdateObject();
		query.addWhereAndField(Keys.LEAGUE_TeamID,new Integer(teamId).toString());
		try{
		ResultSet rs5 = db.query(query);
		while(rs5.next()){
			int played = rs5.getInt(Keys.LEAGUE_Played);
			int wins = rs5.getInt(Keys.LEAGUE_Won);
			int draws = rs5.getInt(Keys.LEAGUE_Drawn);
			int losses = rs5.getInt(Keys.LEAGUE_Lost);
			int points = rs5.getInt(Keys.LEAGUE_Points);
			int goalsFor = rs5.getInt(Keys.LEAGUE_GoalsFor)-aGoalsFor;
			int goalsAgainst = rs5.getInt(Keys.LEAGUE_GoalsAgainst)-aGoalsAgainst;
			played--;
			if(aGoalsFor>aGoalsAgainst){
				wins--;
				update.addField(Keys.LEAGUE_Won,new Integer(wins).toString());
				points = points-3;
				update.addField(Keys.LEAGUE_Points,new Integer(points).toString());
			}else if(aGoalsFor==aGoalsAgainst){
				draws--;
				update.addField(Keys.LEAGUE_Drawn,new Integer(draws).toString());
				points--;
				update.addField(Keys.LEAGUE_Points,new Integer(points).toString());
			}else{
				losses--;
				update.addField(Keys.LEAGUE_Lost,new Integer(losses).toString());
			}
			
			update.addField(Keys.LEAGUE_Played, new Integer(played).toString());
			update.addField(Keys.LEAGUE_GoalsFor,new Integer(goalsFor).toString());
			update.addField(Keys.LEAGUE_GoalsAgainst,new Integer(goalsAgainst).toString());
		}
		db.update(query,update);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	static void updateTeamLeague(DBAccess db, QueryObject query, int aGoalsFor, int aGoalsAgainst, int teamId){
		UpdateObject update = new UpdateObject();
		query.addWhereAndField(Keys.LEAGUE_TeamID,new Integer(teamId).toString());
		try{
		ResultSet rs5 = db.query(query);
		while(rs5.next()){
			int played = rs5.getInt(Keys.LEAGUE_Played);
			int wins = rs5.getInt(Keys.LEAGUE_Won);
			int draws = rs5.getInt(Keys.LEAGUE_Drawn);
			int losses = rs5.getInt(Keys.LEAGUE_Lost);
			int points = rs5.getInt(Keys.LEAGUE_Points);
			int goalsFor = rs5.getInt(Keys.LEAGUE_GoalsFor)+aGoalsFor;
			int goalsAgainst = rs5.getInt(Keys.LEAGUE_GoalsAgainst)+aGoalsAgainst;
			played++;
			if(aGoalsFor>aGoalsAgainst){
				wins++;
				update.addField(Keys.LEAGUE_Won,new Integer(wins).toString());
				points = points+3;
				update.addField(Keys.LEAGUE_Points,new Integer(points).toString());
			}else if(aGoalsFor==aGoalsAgainst){
				draws++;
				update.addField(Keys.LEAGUE_Drawn,new Integer(draws).toString());
				points++;
				update.addField(Keys.LEAGUE_Points,new Integer(points).toString());
			}else{
				losses++;
				update.addField(Keys.LEAGUE_Lost,new Integer(losses).toString());
			}
			
			update.addField(Keys.LEAGUE_Played, new Integer(played).toString());
			update.addField(Keys.LEAGUE_GoalsFor,new Integer(goalsFor).toString());
			update.addField(Keys.LEAGUE_GoalsAgainst,new Integer(goalsAgainst).toString());
		}
		db.update(query,update);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
